package com.share.common.util;

import com.alibaba.fastjson.JSON;
import com.share.common.error.JsonReturnCode;
import com.share.pojo.output.JsonResult;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * web工具类
 *
 * @author liw
 * @version 1.0
 * @date 20227/28 15:20
 */
@Slf4j
public class WebUtils {

    /**
     * 将字符串渲染到客户端
     *
     * @param response 渲染对象
     * @param string   待渲染的字符串
     * @return null
     */
    public static String renderString(HttpServletResponse response, String string) {
        try {
            response.setStatus(200);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().print(string);
        } catch (IOException e) {
            log.error("----WebUtils-----check access fail err ", e);
        }
        return null;
    }

    public static void toReturnJson(HttpServletResponse httpServletResponse, JsonReturnCode jsonReturnCode) {
        log.error("----WebUtils----- login is timeout ");
        JsonResult<String> fail = JsonResult.fail(jsonReturnCode);
        String json = JSON.toJSONString(fail);
        WebUtils.renderString(httpServletResponse, json);

    }

}
